//todo: replace pngimage
unit uMainFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,

  GlScene, GlObjects, GlCadencer, GLWin32Viewer, GLRenderContextInfo, GlWindowsFont, GlHudObjects,
  GlMaterial, GlBitmapFont,
  VectorGeometry,
  uMaterialFont,
  pngimage;
type
  TfrmMain = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    {Main Objects}
    fGlScene: TGlScene;
    fCadencer: TGLCadencer;
    fViewer: TGLSceneViewer;
    fCamera: TGlCamera;
    fMainDummy: TGlDummyCube;
    fFpsTimer: Single;
    fMatLib: TGlMaterialLibrary;

    {Text Objects}
    fWinFont: TGLWindowsBitmapFont;
    fHudText: TGLHUDText;
    fBack: TGlHudSprite;

    fBitmapFont: TMaterialFont;
    fTextField: TGLHUDText;
  public
    Procedure InitFontAndSprites;
    Procedure InitMaterials;
    Procedure OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
  end;

var
  frmMain: TfrmMain;

implementation
uses
  glKeyboard, GLCrossPlatform,
  uDemoInit;
{$R *.dfm}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  fFpsTimer := 0;
  CreateDemo(self, fGlScene, fCadencer, fViewer);
  
  fCamera := TGlCamera.CreateAsChild(fGlScene.Objects);
  with fCamera do
  begin
    Position.SetPoint(0, 0, 5);
    Direction.SetVector(0, 0, -1);
    Up.SetVector(0, 1, 0);
    CameraStyle := csOrthogonal;
    SceneScale := 0.1;
  end;

  fMainDummy := TGLDummyCube.CreateAsChild(fGlScene.Objects);
  fMainDummy.ObjectsSorting := osNone;

  fViewer.Camera := fCamera;
  fCadencer.OnProgress := OnCadencerProgress;

  fMatLib := TGlMaterialLibrary.Create(self);

  InitMaterials;
  InitFontAndSprites;
end;

procedure TfrmMain.FormActivate(Sender: TObject);
begin
  fCadencer.Enabled := true;
end;

Procedure TfrmMain.InitFontAndSprites;
  Function AddSprite(aMatName: String; aX, aY: integer; aW, aH: Integer): TGlHudSprite;
  begin
    result := TGlHudSprite.CreateAsChild(fMainDummy);
    with result do
    begin
      Material.LibMaterialName := aMatName;
      Material.MaterialLibrary := fMatLib;
      Width  := aW;
      Height := aH;
      Position.SetPoint(aX, aY, 0);
    end;
  end;

begin
  fWinFont := TGLWindowsBitmapFont.Create(self);
  fWinFont.Font.Name := 'Arial';

  fBack := AddSprite('back_1', 200, 150, 400, 300);

  fHudText := TGLHUDText.CreateAsChild(fMainDummy);
  with fHudText do
  begin
    BitmapFont := fWinFont;
    Text := 'Lampogolovii presents ''Buttons Demo''';
    Position.SetPoint(0, 5, 0);
    ModulateColor.SetColor(250/255, 205/255, 240/255, 1);
    Alignment := taRightJustify;
  end;
  fHudText.Rotation := 90;


  fBitmapFont := TMaterialFont.Create(self);
  fBitmapFont.Glyphs.LoadFromFile(str_ImagesFolder + '\MaterialFont\font_buttons.png');
  fBitmapFont.fMaterial := fMatLib.Materials.GetLibMaterialByName('Font').Material;
  fBitmapFont.CharHeight := 83;
  fBitmapFont.CharWidth := 57;
  fBitmapFont.VSpace := 1;
  with fBitmapFont.Ranges.Add do
  begin
    StartASCII := 'A';
    StartGlyphIdx := 31;
    StopASCII := 'Z';
  end;
  with fBitmapFont.Ranges.Add do
  begin
    StartASCII := 'a';
    StartGlyphIdx := 63;
    StopASCII := 'd';
  end;
  with fBitmapFont.Ranges.Add do
  begin
    StartASCII := '0';
    StartGlyphIdx := 14;
    StopASCII := '9';
  end;

  fTextField := TGLHUDText.CreateAsChild(fMainDummy);
  fTextField.BitmapFont := fBitmapFont;
  fTextField.Scale.SetVector(0.7, 0.7, 0.7);
  fTextField.Text := 'DEYZabcd';
end;

Procedure TfrmMain.InitMaterials;
begin
  AddMaterials(fMatLib, str_ImagesFolder + '\MaterialFont\',
               ['back_1', 'font_buttons'],
               ['back_1', 'Font']);
end;

Procedure TfrmMain.OnCadencerProgress(Sender : TObject; const deltaTime, newTime : Double);
var
  MPos: TPoint;
begin
  GetCursorPos(Mpos);
  MPos := fViewer.ScreenToClient(MPos);

  fViewer.Invalidate;
end;

end.
